package dj.action;

import java.util.Set;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import dj.Constant;
import dj.action.base.BaseAction;
import dj.model.Tuser;
import dj.model.UserExtend;
import dj.pageModel.Puser;
import dj.pageModel.PuserExtend;
import dj.service.UserService;

@Action("userExtendAction")
public class UserExtendAction extends BaseAction implements ModelDriven<PuserExtend> {

	private PuserExtend puserExtend = new PuserExtend();

	private UserService userService;

	public PuserExtend getModel() {
		return puserExtend;
	}

	public UserService getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * 修改主题
	 */
	public void changeTheme() {
		try {
			Puser puser = (Puser) ServletActionContext.getRequest().getSession().getAttribute(Constant.USER_SESSION);
			Tuser user = userService.getUserById(puser.getId());
			Set<UserExtend> ues = user.getUserExtends();
			UserExtend userExtend;
			if (null != ues && ues.size() > 0) {
				userExtend = ues.iterator().next();
				userExtend.setUitheme(puserExtend.getUitheme());
			} else {
				userExtend = new UserExtend();
				userExtend.setId(UUID.randomUUID().toString());
				userExtend.setTuser(user);
				userExtend.setIconcls("app/user/icons/cat.png");
				userExtend.setUitheme(puserExtend.getUitheme());
				user.getUserExtends().add(userExtend);
			}
			userService.saveOrUpdateExtend(userExtend);
			BeanUtils.copyProperties(userExtend, puser.getPuserExtend());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
